package com.zgc.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zgc.dao.IsourceInfoDao;
import com.zgc.domain.SourceItem;
import com.zgc.domain.TeachSource;

public class IsourceInfoDaoImpl extends HibernateDaoSupport implements IsourceInfoDao{
	@Resource
	public void setSF(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	public void saveSourseInfo( TeachSource teachSource){
		try {
			this.getHibernateTemplate().save(teachSource);
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
	}

	@Override
	public List<TeachSource> qryAllSource() {
		List list = this.getHibernateTemplate().find(" from TeachSource ");
		if (list != null && list.size()>0) {
			return list;
		}else {
			return null;
		}
	}

	@Override
	public List<TeachSource> qryBySpec(String speciality) {
		List<TeachSource> list = new ArrayList<>();
		String hql = " from TeachSource where speciality = ? ";
		speciality = speciality.replace("\"","");
		/*System.out.println(speciality);
		if ("数学".equals(speciality)) {
			System.out.println("sss");
		}*/
		list = this.getHibernateTemplate().find(hql,speciality);
		if (list != null && list.size()>0) {
			return list;
		}else {
			return null;
		}
		
	}

	@Override
	public List<TeachSource> qryByKey(String keyWord) {
		List list = this.getHibernateTemplate().find(" from TeachSource where teacherNm like '%"+keyWord+"%' ");
		if (list != null && list.size()>0) {
			return list;
		}else {
			return null;
		}
	}

	@Override
	public void updateSourById(TeachSource teachSource) {
		this.getHibernateTemplate().update(teachSource);
	}

	@Override
	public TeachSource qrySourceById(int sourceId) {
		return this.getHibernateTemplate().get(TeachSource.class,sourceId);
	}

	@Override
	public void delSourById(int sourceId) {
		TeachSource teachSource = qrySourceById(sourceId);
		/*Integer sourceId2 = teachSource.getSourceId();
		SourceItemDaoImpl sourceItemDaoImpl = new SourceItemDaoImpl();
		sourceItemDaoImpl.q;*/
		/*List<SourceItem> list = teachSource.getList();
		for (SourceItem sourceItem : list) {
			this.getHibernateTemplate().delete(sourceItem);
		}*/
		this.getHibernateTemplate().delete(teachSource);
	}
	@Override
	public int getAllRowCount(String hql) {
		return this.getHibernateTemplate().find(hql).size();
	}

	public List<SourceItem> queryForPage(final String hql, final int offset, final int length) {    
		Session session = this.getSession();    
		Query q = session.createQuery(hql);    
		q.setFirstResult(offset);    
		q.setMaxResults(length);    
		List<SourceItem> list = q.list();    
		System.out.println(list.size());    
//		session.close();    
		return list;    
		}    

}
